define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'user.user/index',
        add_url: 'user.user/add',
        edit_url: 'user.user/edit',
        delete_url: 'user.user/delete',
        modify_url: 'user.user/forbidSendMsg',
        group_auth: 'auth'
    };
    var Controller = {

        index: function () {

            $.ajax({
                type: "GET",
                url: init.group_auth,
                success: function (relus) {
                    ea.table.render({
                        init: init,
                        defaultToolbar:['filter', {
                            title: '搜索',
                            layEvent: 'TABLE_SEARCH',
                            icon: 'layui-icon-search',
                            extend: 'data-table-id="' + 'user' + '"'
                        }],
                        toolbar: ['refresh', 'add', 'delete',],
                        cols: [[
                            {type: "checkbox",width:40},
                            {field: 'username', title: '用户名'},
                            {field: 'group', title: '类型',width:80, search: 'select', selectList: relus},
                            {
                                field: 'rooms',width:80, title: '房间号', templet: function (e) {
                                    if (e.rooms == '') {
                                        return '--';
                                    } else {
                                        return e.rooms
                                    }
                                }
                            },
                            {
                                field: 'extension', title: '归属人', templet: function (e) {
                                    if (e.extension == '') {
                                        return '--'
                                    } else {
                                        return e.extension
                                    }
                                },sort:true,
                            },
                            {
                                field: 'create_home', title: '地区/IP', templet: function (e) {

                                    let html = `<span>IP:` + e.create_ip + `</span><br/><span>`;

                                    if (e.create_home != '') {
                                        html += `地区：` + e.create_home + `</span>`
                                    }
                                    return html;

                                }, search: false
                            },
                            {field: 'create_time', title: '创建时间', search: 'range'},
                            {
                                field: 'online',width:80, title: '状态', templet: function (e) {
                                    if (e.online == 1) {
                                        return `<span>在线</span>`
                                    } else {
                                        return `<span>离线</span>`
                                    }
                                }, search: 'select', selectList: {0: '离线', 1: '在线'}
                            },
                            {field: 'lately_time', title: '最近登录时间', search: 'range'},
                            {
                                field: 'is_forbid',
                                title: '操作',
                                templet: function (e) {
                                    let html = ''
                                    if (e.is_forbid) {
                                        html += `<a class="layui-btn layui-btn-xs layui-btn-warm" data-request="user.user/forbidSendMsg?id=${e.id}&ShutUpTime=0" data-title="确定要取消禁言此用户吗?">解禁</a>`;
                                    } else {
                                        html += `<a class="layui-btn layui-btn-xs layui-btn-danger" data-request="user.user/forbidSendMsg?id=${e.id}" data-title="确定要禁言此用户吗?">禁言</a>`;
                                    }

                                    if (e.is_forbidip) {
                                        html += `<a class="layui-btn layui-btn-xs layui-btn-warm" data-request="user.user/forbidIp?id=${e.id}&type=0" data-title="确定要取消禁此ip吗?">解封</a>`;
                                    } else {
                                        html += `<a class="layui-btn layui-btn-xs layui-btn-danger" data-request="user.user/forbidIp?id=${e.id}" data-title="确定要禁此ip吗?">封IP</a>`;
                                    }
                                    html += `<br/>`
                                    html += `<a class="layui-btn layui-btn-success layui-btn-xs" data-open="user.user/edit?id=${e.id}" data-title="编辑信息">编辑</a>`
                                    html += `<a class="layui-btn layui-btn-danger layui-btn-xs" data-request="user.user/delete?id=${e.id}" data-title="确定删除？">删除</a>`
                                    return html;
                                }
                            },
                            /*{
                                width: 250,
                                title: '操作',
                                templet: ea.table.tool,
                                operat:[
                                    'edit'
                                ]
                            }*/
                        ]],
                    });


                }
            })


            ea.listen();
        },
        add: function () {
            var nowGroup = $("#group").val();
            if (nowGroup == 3 || nowGroup == 6 || nowGroup == 7 || nowGroup == undefined) {
                $("#rooms").hide();
                $("#roomOnly").show();
            } else {
                $("#rooms").show();
                $("#roomOnly").hide();
            }
            layui.use(['form'], function () {
                var form = layui.form;

                form.on('select(group)', function (data) {
                    var group = data.value
                    if (group == 3 || group == 6 || group == 7) {
                        $("#rooms").hide();
                        $("#roomOnly").show();
                    } else {
                        $("#rooms").show();
                        $("#roomOnly").hide();
                    }
                })

            })


            ea.listen();
        },
        edit: function () {

            var nowGroup = $("#group").val();
            if (nowGroup == 3 || nowGroup == 6 || nowGroup == 7) {
                $("#rooms").hide();
                $("#roomOnly").show();
            } else {
                $("#rooms").show();
                $("#roomOnly").hide();
            }
            layui.use(['form'], function () {
                var form = layui.form;
                form.on('select(group)', function (data) {
                    var group = data.value
                    if (group == 3 || group == 6 || group == 7) {
                        $("#rooms").hide();
                        $("#roomOnly").show();
                    } else {
                        $("#rooms").show();
                        $("#roomOnly").hide();
                    }
                })

            })

            ea.listen();
        },
        password: function () {
            ea.listen();
        },
        addall: function () {
            ea.listen();
        }
    };


    return Controller;
});